/**
 * Copyright (C), 2021, 因特驰
 * FileName: BuyerController
 * Author:   李晋武
 * Date:     2021/7/13 17:06
 * Description:
 * History:
 * 李晋武          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.esc.controller;

import com.esc.model.Buyer;
import com.esc.service.BuyerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 〈买家〉<br>
 * 〈〉
 *
 * @author Yun
 * @create 2021/7/13
 * @since 1.0.0
 */
@Controller
@RequestMapping("buyer")
public class BuyerController {

    @Autowired
    private BuyerService buyerService;

    //登录
    @RequestMapping("login")
    @ResponseBody
    public Map<String,String> login(String checkCode, String userPhone, String userPwd, HttpServletRequest request){
        Map<String,String> map = new HashMap<String,String>();
        //校验验证码是否正确
        if(!request.getSession().getAttribute("checkcode").equals(checkCode)){
            map.put("code","1");
            map.put("msg","验证码不正确，请重新输入");
            return map;
        }
        Buyer buyer = buyerService.queryBuyerByPhone(userPhone);
        if(buyer==null){
            map.put("code","2");
            map.put("msg","用户名或密码不正确，请重新输入");
            return map;
        }
        if(!buyer.getBuyerpwd().equals(userPwd)){
            map.put("code","2");
            map.put("msg","用户名或密码不正确，请重新输入");
            return map;
        }
        if(buyer.getStatus().getStatusid()!=1){
            map.put("code","2");
            map.put("msg","该账号"+buyer.getStatus().getStatusname());
            return map;
        }

        request.getSession().setAttribute("buyerName",buyer.getBuyername());
        request.getSession().setAttribute("sellerName","");

        /*if(buyer==null){
            request.getSession().setAttribute("buyerName","-1");
        }else {
            request.getSession().setAttribute("buyerName",buyer.getBuyername());
        }*/

        //校验当前用户是否存在
        /*Admin adminRest = adminService.queryAdminByName(admin.getAdminname());
        if(adminRest==null){
            map.put("code","2");
            map.put("msg","用户名或密码不正确，请重新输入");
            return map;
        }
        //校验密码是否正确(（（用户名+密码）16+盐（用户名+注册时间））32)
        String inputPwd = Md5Util.getMd532(Md5Util.getMd516(admin.getAdminname()+admin.getAdminpwd())+adminRest.getSale()).substring(3,16).replace("-","");
        String getPwd = adminRest.getAdminpwd();
        if (!getPwd.equals(inputPwd)){
            map.put("code","2");
            map.put("msg","用户名或密码不正确，请重新输入");
            return map;
        }*/
        map.put("code","0");
        return map;
    }

    //注册
    @RequestMapping("register")
    @ResponseBody
    public Map<String,String> register(String checkCode,String userName, String userPhone, String userPwd,HttpServletRequest request){
        Map<String,String> map = new HashMap<String,String>();
        //校验验证码是否正确
        if(!request.getSession().getAttribute("checkcode").equals(checkCode)){
            map.put("code","1");
            map.put("msg","验证码不正确，请重新输入");
            return map;
        }
        Buyer buyer = new Buyer();
        buyer.setBuyername(userName);
        buyer.setBuyerphone(userPhone);
        buyer.setBuyerpwd(userPwd);
        buyerService.addBuyer(buyer);
        request.getSession().setAttribute("buyerName","");
        request.getSession().setAttribute("sellerName","");
        map.put("code","0");
        return map;
    }
}